package com.hzw.saas.web.admin.security;

import com.hzw.saas.api.rbac.IUserRoleService;
import com.hzw.saas.api.rbac.bo.RoleBO;
import com.hzw.saas.api.rbac.enums.PermSysEnum;
import com.hzw.saas.api.sys.dto.SysUserAccountDto;
import com.hzw.saas.api.sys.service.ISysUserService;
import com.hzw.saas.api.user.IStaffService;
import com.hzw.saas.api.user.bo.StaffBO;
import com.hzw.saas.common.security.enums.UserType;
import com.hzw.saas.common.security.pojo.dto.SaasUser;
import com.hzw.saas.common.util.AccountValidatorUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 用户信息服务
 *
 * @author zzl
 * @since 12/21/2020
 */
@RequiredArgsConstructor
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {

    private final ISysUserService sysUserService;
    private final IUserRoleService userRoleService;
    private final IStaffService staffService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUserAccountDto userAccount = null;
        // 验证邮箱
        if (AccountValidatorUtil.isEmail(username)) {
            userAccount = sysUserService.get(new SysUserAccountDto().setEmail(username));
        }
        // 验证手机号
        if (userAccount == null && AccountValidatorUtil.isMobile(username)) {
            userAccount = sysUserService.get(new SysUserAccountDto().setPhoneNo(username));
        }
        // 验证用户名
        if (userAccount == null) {
            userAccount = sysUserService.get(new SysUserAccountDto().setUserName(username));
        }

        if (userAccount == null) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }

        Collection<? extends GrantedAuthority> authorities
//            = AuthorityUtils.createAuthorityList(getUserPermissions(userAccount.getUserId()));
            = new ArrayList<>();
        SaasUser saasUser = new SaasUser(userAccount.getUserId(), userAccount.getUserName(), userAccount.getPhoneNo(), userAccount.getEmail()
            , UserType.ADMIN.code(), userAccount.getPassword(), userAccount.getStatus() == 1, authorities);
//        saasUser.setRoles(this.getRoles(userAccount.getUserId()));
//        saasUser.setRealName(this.getRealName(userAccount.getUserName(), userAccount.getPhoneNo(), userAccount.getEmail()));
        return saasUser;
    }

    private String[] getUserPermissions(String userId) {
        List<String> authorities = userRoleService.listAuthorities(PermSysEnum.ADMIN.getCode(), userId);
        return authorities.toArray(new String[0]);
    }

    private List<String> getRoles(String userId) {
        List<RoleBO> roles = userRoleService.listRoles(PermSysEnum.ADMIN.getCode(), userId);
        return roles.stream().map(RoleBO::getName).collect(Collectors.toList());
    }

    private String getRealName(String username, String phoneNo, String email) {
        StaffBO staffBO = staffService.queryByPhoneNo(phoneNo);
        if (Objects.isNull(staffBO)) {
            staffBO = staffService.getByEmail(email);
        }
        return Objects.isNull(staffBO) ? username : staffBO.getStaffName();
    }
}
